package com.wwwarehouse.contract.contract;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.contract.adapter.ReleaseSupplyListRecycleAdapter;
import com.wwwarehouse.contract.bean.ReleaseSupplyGoodsListBean;
import com.wwwarehouse.contract.common.SimpleDividerItemDecoration;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * 发布契约列表详情页
 * Created by ShineF on 2017/10/27 0027.
 */

public class ReleaseSupplyListDetailsFragment extends BaseFragment implements ReleaseSupplyListRecycleAdapter.ItemClickListener {

    private RecyclerView mRvContent;
    private ArrayList<ReleaseSupplyGoodsListBean.ListBean> mDataList;
    private boolean isMeasured = false;
    private ReleaseSupplyListRecycleAdapter mAdapter;
    private int mPage;
    private String mSort;
    private String mSearchText;
    private List<String> mSortContentList;
    private String mType;
    private String mCardUnitUkid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_supply_list_details, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mRvContent = $(R.id.rv_content);
        if (isAdded()) {
//            mRvContent.addItemDecoration(new SpaceItemDecoration(0, 0));
            mRvContent.addItemDecoration(new SimpleDividerItemDecoration(mActivity, 1));
            mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1));
        }
    }

    @Override
    public void setTitle() {
//        mActivity.setTitle(R.string.eidt_release_goods);
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        initData();
        return null;
    }

    public void initData() {
        mDataList = new ArrayList<>();
        mSortContentList = new ArrayList<>();
        Bundle bundle = getArguments();
        if (null != bundle) {
            mSort = bundle.getString("sortName");
            String mEffectiveStatus = bundle.getString("status");
            mSearchText = bundle.getString("searchText");
            mType = bundle.getString("type");
            mCardUnitUkid = bundle.getString("cardUnitUkid");
            mPage = bundle.getInt("page");
            mDataList = bundle.getParcelableArrayList("dataList");
            mSortContentList = bundle.getStringArrayList("filt");
            //服务器在首次请求会把第一页数据回传过来, 所以本地做是否是第一页的验证
            if (null != mDataList && mPage == 1) {
                final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (!isMeasured) {
                            setHeight(mRvContent.getMeasuredHeight());
                        }
                        observer.removeOnGlobalLayoutListener(this);
                    }
                });
            } else {
                requestHttp(mEffectiveStatus);
            }
        }
    }

    public void requestHttp(String status) {
        Map<String, Object> map = new HashMap<>();
        map.put("contractStatus", status);
        map.put("demanderBusinessName", mSearchText);
        map.put("page", mPage);
        map.put("size", 7);
        map.put("sort", mSort);
        map.put("supplierBusinessIds", mSortContentList);
        loadData(ContractConstant.RELEASE_GOODS_LIST, map, 1);
    }

    public void setHeight(int height) {
        isMeasured = true;
        if (mAdapter == null && isAdded() && null != mDataList) {
            mAdapter = new ReleaseSupplyListRecycleAdapter(mActivity, mDataList, height, mType);
            mRvContent.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
        }
    }

    @Override
    public void clickListener(int i, ReleaseSupplyGoodsListBean.ListBean bean, View view) {
        ReleaseSupplyCardFragment fragment = new ReleaseSupplyCardFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("data", bean);
        bundle.putInt("type", 2);
        bundle.putString("type", mType);
        bundle.putString("cardUnitUkid", mCardUnitUkid);
        fragment.setArguments(bundle);
        pushFragment(fragment);
    }

    public void loadData(String url, Map map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int what) {
                if (what == 1) {
                    ReleaseSupplyGoodsListBean bean = JSON.parseObject(commonClass.getData().toString(), ReleaseSupplyGoodsListBean.class);
                    if (bean != null) {
                        setData(bean);
                    }
                }
            }

            @Override
            public void onFailed(String result, int what) {

            }

            @Override
            public void onFinish(int what) {

            }
        }, what);
    }

    public void setData(ReleaseSupplyGoodsListBean bean) {
        if (bean.getList() == null) {
            return;
        }
        if (bean.getList().size() == 0) {
            return;
        }
        mDataList = bean.getList();
        ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (!isMeasured) {
                    setHeight(mRvContent.getMeasuredHeight());
                }
                return true;
            }
        });
    }
}
